package com.duan.ad;

import com.duan.ad.pojo.Ad;
import com.duan.ad.service.AdService;
import com.duan.entity.PageResult;
import com.duan.entity.Result;
import com.duan.entity.StatusCode;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告表
 */
@RestController
@RequestMapping("/Ad")
public class AdController {
    @Autowired
    private AdService adService;


    /**
     * 查询全部
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Ad>> selectAll(){
        return Result.ok("查询成功", adService.selectAll());
    }


    /**
     * 条件查询
     * @param ad
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<Ad> > select(@RequestBody Ad ad){
        return Result.ok("条件查询成功", adService.selectList(ad));
    }

    /**
     * 新增广告
     * @param ad
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Ad ad){
        adService.insert(ad);
        return Result.ok("新增成功");
    }


    /**
     * 删除广告
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        adService.delete(ids);
        return Result.ok("删除成功");
    }


    /**
     * 修改广告
     * @param ad
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Ad ad){
        adService.update(ad);
        return Result.ok("修改成功");
    }

    /**
     * 分页查询
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    @GetMapping(value = "/pageAll" )
    public Result pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Ad> pageList = adService.pageAll(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
     * 分页条件查询
     * @param ad
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody Ad ad,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Ad> pageList = adService.pageList(ad,page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }
}
